#include <stdio.h>

float add(int a, int b);

float sub(int a, int b);

float mul(int a, int b);

float div(int a, int b);

float compute(int a, int b, float (*p)(int, int));

int myatoi(char* str);

int main(int argc, char** argv)
{
	int a, b;
	float (*func)(int, int) = NULL;
	float res;

	// 0     1     2   3
	// name, num1, op, num2
	if (argc < 4) {
		printf("输入格式有误\n");
		return -1;
	}

	a = myatoi(argv[1]);
	b = myatoi(argv[3]);

	switch (argv[2][0]) {
		case '+':
			func = add;
			break;
		case '-':
			func = sub;
			break;
		case 'x':
			func = mul;
			break;
		case '/':
			func = div;
			break;
		default:
			printf("不支持\"%s\"运算符\n", argv[2]);
			return -1;
	}

	res = compute(a, b, func);

	printf("%d %s %d = %.2f\n", a, argv[2], b, res);

	return 0;
}

float add(int a, int b)
{
	return a + b;
}

float sub(int a, int b)
{
	return a - b;
}

float mul(int a, int b)
{
	return a * b;
}

float div(int a, int b)
{
	return a / (float) b;
}

float compute(int a, int b, float (*p)(int, int))
{
	float res;

	res = p(a, b);

	return res;
}

static int is_digit(char c)
{
	return c >= '0' && c <= '9';
}

static int get_digit(char c)
{
	return c - '0';
}

int myatoi(char* str)
{
	int i = 0;
	int num = 0;

	while (str[i] && is_digit(str[i])) {
		num = num * 10 + get_digit(str[i]);
		i++;
	}

	return num; 
}
